<?php

/*
 * Description of tour_model
 * @author Pham Minh Khoi
 * @email: pmkhoi@gmail.com
 * @package: 
 */


class Tour_model extends CI_Model {

    //put your code here
    function __constructor() {
        parent::__construct();
    }

    function create($category) {

        $sql = "INSERT INTO `gtb_tour` " .
                "(`code`, `vi_name`, `en_name`, `vi_summary`, `en_summary`, `vi_description`, " .
                "`en_description`, `price`, `departure`, `period`, `province_id`, `tour_category_id`, `tour_type_id`, `friendly_url`, `position`, " .
                "`date_add`, `date_upd`, `active`, `special`, `best_sell`, " .
                " `image`, `video`, `visited`) " .
                "VALUES ";

        $sql .= "(" .
                $this->db->escape($category['code']) . ", " .
                $this->db->escape($category['vi_name']) . ", " .
                $this->db->escape($category['en_name']) . ", " .
                $this->db->escape($category['vi_summary']) . ", " .
                $this->db->escape($category['en_summary']) . ", " .
                "'" . ($category['vi_description']) . "', " .
                "'" . ($category['en_description']) . "', " .
                $this->db->escape($category['price']) . ", " .
                $this->db->escape($category['departure']) . ", " .
                $this->db->escape($category['period']) . ", " .
                intval($category['province_id']) . ", " .
                intval($category['tour_category_id']) . ", " .
                intval($category['tour_type_id']) . ", " .
                $this->db->escape($category['friendly_url']) . ", " .
                intval($category['position']) . ", " .
                $this->db->escape($category['date_add']) . ", " .
                $this->db->escape($category['date_upd']) . ", " .
                intval($category['active']) . ", " .
                intval($category['special']) . ", " .
                intval($category['best_sell']) . ", " .
                $this->db->escape($category['image']) . ", " .
                $this->db->escape($category['video']) . ", " .
                intval($category['visited']) . 
                ");";

        $this->db->query($sql);
        //return $this->db->affected_rows();
        return $this->db->insert_id();
    }

    function update($category) {

        $sql = "UPDATE `gtb_tour` SET" .
                " code = " . $this->db->escape($category['code']). ",".
                " vi_name = " . $this->db->escape($category['vi_name']) . ", " .
                " en_name = " . $this->db->escape($category['en_name']) . ", " .
                " vi_summary = " .  $this->db->escape($category['vi_summary']) . ", " .
                " en_summary = " .  $this->db->escape($category['en_summary']) . ", " .
                " vi_description = '" . ($category['vi_description']) . "', " .
                " en_description = '" . ($category['en_description']) . "', " .
                " price = " . $this->db->escape($category['price']) . ", " .
                " departure = '" . ($category['departure']) . "', " .
                " period = '" . ($category['period']) . "', " .
                " province_id = " . intval($category['province_id']) . ", " .
                " tour_category_id = " . intval($category['tour_category_id']) . ", " .
                " tour_type_id = " .intval($category['tour_type_id']) . ", " .
                " friendly_url = " . $this->db->escape($category['friendly_url']) . ", " .
                " position = " . intval($category['position']) . ", " .
                " date_add = " . $this->db->escape($category['date_add']) . ", " .
                " date_upd = " .$this->db->escape($category['date_upd']) . ", " .
                " active = " .intval($category['active']) . ", " .
                " special = ".  intval($category['special']) . ", " .
                " best_sell = " . intval($category['best_sell']) . ", " .
                " image = " . $this->db->escape($category['image']) . ", " .
                " video = " . $this->db->escape($category['video']) . ", " .
                " visited = " . intval($category['visited']).
                " WHERE id = " . intval($category['id']);

        $this->db->query($sql);
        return $this->db->affected_rows();
        //return $this->db->insert_id();
    }

    function getAll($offset = 0, $limit = NUMBER_RECORD_PER_PRODUCT_PAGE, $para = '') {
        $sql = "SELECT * FROM gtb_tour
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $this->db->query($sql);

        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    function countAll($para = '') {
        $sql = "SELECT count(*) as number_record
		FROM gtb_tour
		";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->number_record;
        }
    }

    function getMaxPosition() {
        $sql = "SELECT max(position) as max_position
		FROM gtb_tour
		";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->max_position;
        }
    }

    function findByID($id) {
        $sql = "SELECT * 
		FROM gtb_tour
                WHERE id = " . intval($id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }
        return null;
    }

    function getByFriendlyUrl($link_rewrite){

		$sql = "SELECT *
				FROM gtb_tour
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function getByCode($code){

		$sql = "SELECT *
				FROM gtb_tour
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function setImage($image, $tourID){
            $sql = "UPDATE gtb_tour SET image = ". $this->db->escape($image).
                   " WHERE id = " . intval($tourID);
            $query = $this->db->query($sql);
            return $this->db->affected_rows();
        }

        function findByCategory($cid, $offset = 0, $limit = 10) {
        $sql = "SELECT *
		FROM gtb_tour
                WHERE tour_category_id IN( " . $cid .") AND (special = 1 OR best_sell = 1)
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result;
        }
    }
}

?>
